<template>
    <div class="box">
        <div class="floatBtn">
            <el-button icon="el-icon-refresh" size="mini" type="text" @click="getGtList"></el-button>
        </div>
        <div class="title">港通(沪/深/总)</div>
        <div class="list moneyList" ref="gtEl">
            <el-row v-for="(item,idx) in gtList" :key="idx" class="mt2">
                <span class="colspan">{{item.col1}}</span>
                <span class="colspan" :class="item.col2color">{{item.col2}}</span>
                <span class="colspan" :class="item.col3color">{{item.col3}}</span>
                <span class="colspan" :class="item.col4color">{{item.col4}}</span>
            </el-row>
        </div>
    </div>
</template>

<script>

export default {
    data() {
        return {
            gtList:[],//港通数据
            curDate: this.$dayjs().format("MM-DD"),
            gangtong:"http://push2.eastmoney.com/api/qt/kamt.rtmin/get?fields1=f1,f2,f3,f4&fields2=f51,f52,f53,f54,f55,f56",
            timer: null
        }
    },
    inject: ['cfg',"isQry","getSleepTime","isMoneyException","addException"],
    

    mounted() {
        this.startTimer();
    },

    methods: {
        startTimer(){
            this.getGtList();
            this.$once('hook:beforeDestroy', () => {   
                clearTimeout(this.timer);
            })
            this.doTimer();
        },
        doTimer(){
            clearTimeout(this.timer);
            if(this.isQry()){
                this.getGtList();
            }
            var sleep = this.getSleepTime();

            this.timer = setTimeout(()=>{
                this.doTimer();
            },sleep);
        },
        getGtList(){
            this.$axios.get(this.gangtong).then((res) => {
                if(res.data && res.data.data && res.data.data.s2n){
                    var datas = res.data.data.s2n;
                    if(res.data.data.s2nDate!=this.curDate){
                        this.gtList = [{
                            col1: "今日",
                            col2: "无数据",
                            col3: "最近",
                            col4: res.data.data.s2nDate,
                        }];
                        return;
                    }
                    var list = [];
                    var preItem = null;
                    datas.map((item)=>{
                        var arr = item.split(",")
                        if(arr.length<2 || isNaN(arr[1])) return;
                        var item = {
                            col1: arr[0],
                            col2: this.formatWanMoney(arr[1]),
                            col3: this.formatWanMoney(arr[3]),
                            col4: this.formatWanMoney(arr[5]),
                        }
                        // if(item.col1>"11:22")return;
                        if(preItem){
                            item.col2color=this.getColor(item.col2,preItem.col2)
                            item.col3color=this.getColor(item.col3,preItem.col3)
                            item.col4color=this.getColor(item.col4,preItem.col4)

                            item.col2up = item.col2>preItem.col2;
                            item.col3up = item.col3>preItem.col3;
                            item.col4up = item.col4>preItem.col4;
                        }
                        list.push(item);
                        preItem = item;
                    });
                    this.gtList = list;
                    this.$nextTick(()=>{
                        this.$refs.gtEl.scrollTop = this.$refs.gtEl.scrollHeight;
                    });
                    if(this.exceptionTime){
                        var cur =  new Date().getTime();
                        if(cur-this.exceptionTime<30000) return;
                    };
                    if(!this.cfg.event.gt_money) return;
                    //判断是否需要提示数据异常
                    if(list<5 || list[list.length-1].col1=="15:00") return;
                    if(this.isMoneyException(list,"col4up","港通")){
                        this.exceptionTime = new Date().getTime();
                        return;
                    }
                }else{
                    this.$message.error(JSON.stringify(res.data));
                }
            })

            
        },
    }
}
</script>


